﻿using System.Collections.Generic;
using Core;
using Core.CSV;
using Core.Event;
using UnityEngine;
namespace Game.UI.Weapon {
    public class WeaponEditWindow : UIWindow {
        public WeaponArea WeaponArea;
        [SerializeField]
        private WeaponEffectListComponent mCurWeaponEffectList;
        
        [SerializeField]
        private WeaponEffectListComponent mEffectBagList;
        /// <summary>
        /// 当前武器的武器效果列表
        /// </summary>
        private List<uint> mEffectIDList;

        /// <summary>
        /// 当前武器效果背包的武器效果列表
        /// </summary>
        private List<uint> mEffectBagIDList;
        
        protected override void onInit() {
            //获取当前玩家
            Player curPlayer = PlayerManager.Ins.CurPlayer;
            Game.Weapon curWeapon = curPlayer.EquipModule.GetWeapon();
            //初始化列表
            mEffectIDList = ListUtils.CreateList<uint>(curWeapon.Data.EffectCapacity);
            mEffectBagIDList = ListUtils.CreateList<uint>(curPlayer.BagModule.WeaponEffectBagCapacity);
            
            mCurWeaponEffectList.OnSlotItemChanged = (slot) => {
                //获得当前武器
                Game.Weapon curWeapon = PlayerManager.Ins.CurPlayer.EquipModule.GetWeapon();
                if (slot.Data == null) {
                    curWeapon.RemoveEffect(slot.Index);    
                } else {
                    curWeapon.InsertEffect(WeaponEffectFactory.New(slot.Data),slot.Index);    
                }
            };

            mEffectBagList.OnSlotItemChanged = (slot) => {
                if (slot.Data == null) {
                    //背包清理
                    PlayerManager.Ins.CurPlayer.BagModule.WeaponEffectBag.RemoveEffect(slot.Index);
                } else {
                    PlayerManager.Ins.CurPlayer.BagModule.WeaponEffectBag.InsertEffect(slot.Index,slot.Data.ID);
                }
            };
        }

        protected override void onOpen(object[] args) {
            GameEventMgr.Ins.RegisterEvent(GameEventID.SwitchWeapon, onSwitchWeapon);
            
            checkUpdate();
            refreshWeaponEffectList();
            refreshWeaponEffectBagList();
            
            WeaponArea.Refresh();
        }

        protected override void onClose() {
            GameEventMgr.Ins.UnRegisterEvent(GameEventID.SwitchWeapon, onSwitchWeapon);
        }

        private void onSwitchWeapon(GameEvent evt) {
            checkUpdate();
            refreshWeaponEffectList();
        }

        private void checkUpdate() {
            int weaponEffectListCount = PlayerManager.Ins.CurPlayer.EquipModule.GetWeapon().GetWeaponEffectList().Count;
            if (weaponEffectListCount != mEffectIDList.Count) {
                mEffectIDList = ListUtils.CreateList<uint>(weaponEffectListCount);
            }
            int weaponEffectListBagCount = PlayerManager.Ins.CurPlayer.BagModule.WeaponEffectBagCapacity;
            if (weaponEffectListBagCount != mEffectBagIDList.Count) {
                mEffectBagIDList = ListUtils.CreateList<uint>(weaponEffectListBagCount);
            }
        }
        
        private void refreshWeaponEffectList() {
            //更新当前武器的效果列表
            var weaponEffectList = PlayerManager.Ins.CurPlayer.EquipModule.GetWeapon().GetWeaponEffectList();
            for (int i = 0; i < weaponEffectList.Count; ++i) {
                mEffectIDList[i] = weaponEffectList[i]?.Data.ID ?? default(uint);
            }
            mCurWeaponEffectList.RefreshData(mEffectIDList);
        }

        private void refreshWeaponEffectBagList() {
            //更新背包的效果列表
            mEffectBagIDList = ListUtils.CreateList<uint>(PlayerManager.Ins.CurPlayer.BagModule.WeaponEffectBagCapacity);
            var effectBagList = PlayerManager.Ins.CurPlayer.BagModule.GetWeaponEffectList();
            for (int i = 0; i < effectBagList.Count; ++i) {
                mEffectBagIDList[i] = effectBagList[i];
            }
            mEffectBagList.RefreshData(mEffectBagIDList);
        }
    }
}